package org.bp.util;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

public class RandomUtils {
	/**
	 * 获取m 到 n 之间的随机数 包括m n
	 * 
	 * @param m
	 * @param n
	 * @return
	 */
	public static int getRandomBetweenMAndN(int m, int n) {
		if (n < m) {
			return -1;
		}
		Random random = new Random();
		return random.nextInt(n + 1 - m) + m;
	}

	/**
	 * 获取m - n 之间的 num 个 不重复的随机数
	 * 
	 * @param m
	 * @param n
	 * @param num
	 * @return
	 */
	public static Set<Integer> getRandoms(int m, int n, int num) {
		if (n < m || num > (n-m)) {
			return null;
		}
		Set<Integer> set = new HashSet<>(num);
		Random random = new Random();
		while (set.size() != num) {
			set.add(random.nextInt(n + 1 - m) + m);
		}
		return set;
	}

	/**
	 * 获取 num 个 不重复的随机数
	 * 
	 * @param num
	 * @return
	 */
	public static Set<Integer> getRandoms(int num) {
		Set<Integer> set = new HashSet<>(num);
		Random random = new Random();
		while (set.size() != num) {
			set.add(random.nextInt());
		}
		return set;
	}

	public static void main(String[] args) {
		System.out.println(getRandoms(100,0,20));
	}
}
